package com.orange.adminapi.controller.activity;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.entity.activity.dto.ActivityRegistrationAuditDTO;
import com.orange.core.pojo.entity.activity.vo.RegistrationVO;
import com.orange.core.service.activity.ActivityService;
import com.orange.core.pojo.BR;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.activity.dto.ActivityDTO;
import com.orange.core.pojo.entity.activity.vo.ActivityVO;
import com.orange.core.pojo.req.activity.ActivityPageReq;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * <p>ClassName:ActivityController</p>
 * <p>Description: 活动管理controller</p>
 * <p>Author: lwm</p>
 * <p>Date: 2025/5/22 23:19</p>
 */

@RequiredArgsConstructor
@RestController
@RequestMapping("/activity")
public class ActivityController {

    private final ActivityService activityService;

    /**
     * 活动管理分页查询
     * @param pageReqDto
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_LIST.permission.identifier)")
    @PostMapping("/page")
    public R<Page<ActivityVO>> page(@RequestBody PageReqDto<ActivityPageReq> pageReqDto) {
        Page<ActivityVO> page = activityService.page(pageReqDto);
        return R.ok(page);
    }

    /**
     * 活动管理详情
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_INFO.permission.identifier)")
    @GetMapping("/info/{id}")
    public R<ActivityVO> info(@PathVariable("id") int id){
        return R.ok(activityService.info(id));
    }

    /**
     * 活动管理新增
     * @param activityDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_SAVE.permission.identifier)")
    @PostMapping
    public R<Boolean> save(@RequestBody @Validated ActivityDTO activityDTO){
        return BR.result(activityService.save(activityDTO));
    }

    /**
     * 活动管理修改
     * @param activityDTO
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_UPDATE.permission.identifier)")
    @PutMapping
    public R<Boolean> update(@RequestBody @Validated ActivityDTO activityDTO){
        return BR.result(activityService.update(activityDTO));
    }

    /**
     * 活动管理批量删除
     * @param ids
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_DELETE.permission.identifier)")
    @DeleteMapping("/{ids}")
    public R<Boolean> remove(@PathVariable("ids") Integer[] ids){
        return BR.result(activityService.deleteActivityByIds(ids));
    }

    /**
     * 设置热门活动
     * @param activityId
     * @param isHot
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_UPDATE.permission.identifier)")
    @PostMapping("/setHotActivity/{activityId}/{isHot}")
    public R<Boolean> setHotActivity(@PathVariable("activityId") Integer activityId,@PathVariable("isHot") Integer isHot){
        return R.ok(activityService.setHotActivity(activityId,isHot));
    }

    /**
     * 获取报名人员信息
     * @param activityId
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_INFO.permission.identifier)")
    @GetMapping("/registration/{activityId}")
    public R<RegistrationVO> registration(@PathVariable("activityId") Integer activityId){
        return R.ok(activityService.registration(activityId));
    }

    /**
     * 审核报名人员状态
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_UPDATE.permission.identifier)")
    @PostMapping("/audit")
    public R<Boolean> audit(@RequestBody @Validated ActivityRegistrationAuditDTO auditDTO){
        return BR.result(activityService.audit(auditDTO));
    }

    /**
     * 获取活动列表
     * @return
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_LIST.permission.identifier)")
    @GetMapping("/list")
    public R<List<ActivityVO>> activityList(){
        return R.ok(activityService.activityList());
    }

    /**
     * 报名人员信息导出
     * @param response
     * @param request
     */
    @PreAuthorize("hasAuthority(T(com.orange.core.pojo.enums.EPermission).ACTIVITY_EXPORT.permission.identifier)")
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody Map<String,Object> request){
        Integer activityId = (Integer)request.get("activityId");
        activityService.export(response,activityId);
    }
}
